package name.ekr.edu.threads.chatserver.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static name.ekr.edu.threads.chatserver.Constants.*;


public class Connection {
	private static Logger logger = LoggerFactory.getLogger(Connection.class);
	private Socket socket = null;
	private BufferedReader connectionReader = null;
	private PrintWriter connectionWriter = null;
	
	
	public Connection(Socket socket) {
		this.socket = socket;
		try {
			connectionReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), ENCODING));
			connectionWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), ENCODING), true);
		} catch (UnsupportedEncodingException e) {
			logger.error("", e);
			throw new RuntimeException("Can't use default encodind " + ENCODING);
		} catch (IOException e) {
			logger.error("", e);
			throw new RuntimeException("Can't communicate through connection");
		}
	}

	
	public BufferedReader getConnectionReader() {
		return connectionReader;
	}

	public PrintWriter getConnectionWriter() {
		return connectionWriter;
	}
}
